package cn.leancloud.leanstoragegettingstarted;

import android.content.Context;
import android.content.Intent;
import android.os.Environment;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.DeleteCallback;
import com.avos.avoscloud.GetCallback;
import com.avos.avoscloud.GetDataCallback;
import com.avos.avoscloud.ProgressCallback;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;

public class MainRecyclerAdapter extends RecyclerView.Adapter<MainRecyclerAdapter.ViewHolder> {
  private Context mContext;
  private List<AVObject> mList;

  public MainRecyclerAdapter(List<AVObject> list, Context context) {
    this.mContext = context;
    this.mList = list;
  }

  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    return new ViewHolder(LayoutInflater.from(mContext).inflate(R.layout.data_item, parent, false));
  }


  public void onBindViewHolder(ViewHolder holder, final int position) {

    holder.fname.setText((CharSequence) mList.get(position).get("文件名称"));
    holder.fsize.setText(mList.get(position).get("文件大小") == null ? "M" : "M " + mList.get(position).get("文件大小"));
    //holder.mName.setText(mList.get(position).getAVUser("owner") == null ? "" : mList.get(position).getAVUser("owner").getUsername());
    //Picasso.with(mContext).load(mList.get(position).getAVFile("image") == null ? "www" : mList.get(position).getAVFile("image").getUrl()).transform(new RoundedTransformation(9, 0)).into(holder.mPicture);

    holder.btn_download.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        AVQuery<AVObject> query = new AVQuery<>("_File");
        query.getInBackground("", new GetCallback<AVObject>() {
          public void done(AVObject avObject, AVException e) {
            AVFile file = new AVFile("test.txt",avObject.getString("url"), new HashMap<String, Object>());
            file.getDataInBackground(new GetDataCallback() {
              public void done(byte[] bytes, AVException e) {
                if (e == null) {
                  Log.d("saved", "文件大小" + bytes.length);
                } else {
                  Log.d("saved", "出错了" + e.getMessage());
                }
                File downloadedFile = new File(Environment.getExternalStorageDirectory() + "/test.png");
                FileOutputStream fout = null;
                try {
                  fout = new FileOutputStream(downloadedFile);
                  fout.write(bytes);
                  Log.d("saved", "文件写入成功.");
                  fout.close();
                } catch (FileNotFoundException e1) {
                  e1.printStackTrace();
                  Log.d("saved", "文件找不到.." + e1.getMessage());
                } catch (IOException e1) {
                  Log.d("saved", "文件读取异常.");
                }
              }
            }, new ProgressCallback() {
              @Override
              public void done(Integer integer) {
                Log.d("saved", "文件下载进度" + integer);
              }
            });
          }
        });

      }
    });
    holder.btn_delect.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View view) {
        AVQuery<AVObject> query = new AVQuery<>("_File");
        query.getInBackground("", new GetCallback<AVObject>() {
          public void done(AVObject avObject, AVException e) {
            AVFile file = new AVFile("", avObject.getString("url"), new HashMap<String, Object>());
            file.deleteInBackground(new DeleteCallback() {
              @Override
              public void done(AVException e) {

              }
            });
          }
        });
      }
    });

  }

  @Override
  public int getItemCount() {
    return mList.size();
  }

  public class ViewHolder extends RecyclerView.ViewHolder {

    TextView fname;
    TextView fsize;
    Button btn_download;
    Button btn_delect;

    public ViewHolder(View itemView) {
      super(itemView);
      fname = (TextView) itemView.findViewById(R.id.fname);
      fsize = (TextView) itemView.findViewById(R.id.fsize);
      btn_download = (Button) itemView.findViewById(R.id.btn_download);
      btn_delect = (Button) itemView.findViewById(R.id.btn_delect);
    }
  }
}

